﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculateInfiniteConvergents
{
    class CalculateInfiniteConvergents
    {
        public delegate void InfiniteConvergenceDelegate();

        public static void CalculateType1()
        {
            decimal sum = 1;
            decimal toAdd = 0.5M;
            int counter = 1;

            while (toAdd > 0.000001M)
            {
                toAdd = (decimal)(1 / (Math.Pow(2, counter)));
                sum += toAdd;
                counter++; 
            }

            Console.WriteLine("{0:F2}",sum);
        }

        public static void CalculateType2()
        {
            decimal sum = 1;
            decimal toAdd = 1M;
            int counter = 2;

            while (toAdd > 0.000001M)
            {
                toAdd *=(decimal) 1 / counter;
                sum += toAdd;
                counter++;
            }

            Console.WriteLine("{0:F2}", sum);
        }

        public static void CalculateType3()
        {
            decimal sum = 1;
            decimal toAdd = 0.5M;
            int counter = 1;

            while (toAdd > 0.000001M)
            {
                toAdd = (decimal)(1 / (Math.Pow(2, counter)));
                if (counter % 2 == 1)
                {
                    sum += toAdd;
                }
                else
                {
                    sum -=toAdd;
                }
                counter++;
            }

            Console.WriteLine("{0:F2}", sum);
        }


        static void Main()
        {
            InfiniteConvergenceDelegate testDelegate = new InfiniteConvergenceDelegate(CalculateType1);
            testDelegate += CalculateType2;
            testDelegate += CalculateType3;
            testDelegate();
        }
    }
}
